package cn.skyquant.quant4j.task.boot.bo;

import cn.skyquant.quant4j.task.boot.entity.Test;
import cn.skyquant.quant4j.task.boot.repository.TestRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import org.springframework.transaction.annotation.Transactional;
import java.util.Random;


@Service
public class TestBO {

    @Autowired
    TestRepository testRepository;

    @Transactional
    public int[] insertWithTrans() {
        //先插入一条正确的
        Test t1 = new Test();
        t1.i = new Random().nextInt(10);
        t1.name = "trans";
        testRepository.insert(t1);

        //再插入一条错误的

        Test t2 = new Test();
        t2.i = new Random().nextInt(10);
//        t2.name = "trans";
        testRepository.insert(t2);

        return new int[]{t1.i, t2.i};
        //理论上两条都不能插入成功
    }


    public int[] insertWithoutTrans() {
        //先插入一条正确的
        Test t1 = new Test();
        t1.i = new Random().nextInt(10);
        t1.name = "hello";
        testRepository.insert(t1);

        //再插入一条错误的

        Test t2 = new Test();
        t2.i = new Random().nextInt(10);
        testRepository.insert(t2);

        return new int[]{t1.i, t2.i};
        //理论上两条都不能插入成功
    }
}
